package com.base.cn.platform.os.controller.web.user.address;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.manage.area.SysArea;
import com.base.cn.platform.os.service.manage.area.SysAreaService;
import com.base.cn.platform.os.service.manage.user.address.CusUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户地址
 * @author ll
 * @date 2018/5/28 14:28
 */
@Controller
@RequestMapping("/web/uc")
public class WebUserAddressController extends WebBaseController {

    @Autowired
    private CusUserAddressService cusUserAddressService;
    @Autowired
    private SysAreaService sysAreaService;

    /**
     * 初始化用户地址添加/修改页面
     * @param model Model
     * @param addressId 地址ID
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam("addressId") BigDecimal addressId){
        Map<String,Object> address = null;
        //查询省列表
        SysArea sysArea = new SysArea();
        sysArea.setAreaType(0);
        sysArea.setParentId(BigDecimal.ZERO);
        List<SysArea> provinceList = sysAreaService.queryUserAreaList(sysArea);
        List<SysArea> cityList = null;
        List<SysArea> townList = null;
        if(DataUtil.idIsNotNull(addressId)){
            //查询用户地址列表
            address = cusUserAddressService.queryUserAddressById(addressId,true);
            //查询市县列表
            String parentIds = address.get("provinceId") + "," + address.get("cityId");
            Map<BigDecimal,List<SysArea>> listMap = sysAreaService.querySysAreaMapByParentIds(parentIds);
            if(ObjectUtils.isNotEmpty(listMap)){
                cityList = listMap.get(new BigDecimal(address.get("provinceId").toString()));
                townList = listMap.get(new BigDecimal(address.get("cityId").toString()));
            }
        }
        if(address==null){
            address = new HashMap<>();
        }
        model.addAttribute("address",address);
        model.addAttribute("provinceList",provinceList);
        model.addAttribute("cityList",cityList);
        model.addAttribute("townList",townList);
        return resultPage("/order/goods/init-address");
    }

    /**
     * 添加用户地址
     *
     * @param request
     * @param userAddress 用户收货地址
     * @return
     */
    @RequestMapping("/ajax/saveUserAddress")
    @ResponseBody
    public Map<String, Object> saveUserAddress(HttpServletRequest request,
                                                  @RequestBody Map<String,Object> userAddress) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        userAddress.put("userId",userId);
        return cusUserAddressService.saveUpdateCusUserAddress(userAddress);
    }



    /**
     * 查询行政区划
     *
     * @param parentId 父行政区划id
     * @param areaType 0.省级 2.市级 3.县级 (4.乡镇级 5.村级)
     */
    @RequestMapping("/ajax/queryAreaListByParentId")
    @ResponseBody
    public Map<String, Object> queryAreaListByParentId(
            @RequestParam("parentId") BigDecimal parentId,
            @RequestParam("areaType") int areaType) {
        SysArea sysArea = new SysArea();
        sysArea.setParentId(parentId);
        sysArea.setAreaType(areaType);
        List<SysArea> areaMapList = sysAreaService.queryUserAreaList(sysArea);
        return ResultUtil.SUCCESS("操作成功", areaMapList);
    }


    /**
     * 修改状态
     *
     * @param addressId
     * @return
     */
    @RequestMapping("/ajax/updateUserAddressStatus")
    @ResponseBody
    public Map<String, Object> updateUserAddressStatus(@RequestParam("addressId") BigDecimal addressId,
                                                       @RequestParam("status") Integer status) {
        cusUserAddressService.updateUserAddressIsFirst(addressId, status);
        return ResultUtil.SUCCESS("操作成功", null);
    }

    /**
     * 修改页面
     *
     * @param addressId
     * @return
     */
    @RequestMapping("/ajax/queryUserAddressById")
    @ResponseBody
    public Map<String, Object> queryUserAddressById(@RequestParam(value = "addressId", required = false) BigDecimal addressId) {
        Map<String,Object> userAddress = new HashMap<>();
        if (DataUtil.idIsNotNull(addressId)) {
            userAddress =cusUserAddressService.queryUserAddressById(addressId,true);
        }
        return ResultUtil.SUCCESS("操作成功", userAddress);
    }


    /**
     * 删除用户地址
     *
     * @param addressId 地址Id
     * @return
     */
    @RequestMapping("/ajax/deleteUserAddressById")
    @ResponseBody
    public Map<String, Object> deleteUserAddressById(@RequestParam("addressId") BigDecimal addressId) {
        return  cusUserAddressService.deleteUserAddressById(addressId);
    }
}
